
const  Block =qguan("Block1")
/**
 * 文件
 */

export default class File1{
    constructor(){
        this.list=[]
        this.index=0;
    }

    /**
     *  加载 数据
     * @param data
     */
    loadData(data){
        let _this=this;
        let rows=[];
        let testNub="";
        for(let i=0;i<data.length;i++){
            let num=data[i].toString(2);
            let buffNum =  (Array(8).join(0) + num).slice(-8);
            let code0= parseInt(buffNum.substr(0,4),2).toString(16);
            let code1= parseInt(buffNum.substr(4,7),2).toString(16);
            testNub+=code0+code1
        }
       let list= testNub.split("c")
        let regex=/^[0]+/;
        if(list[list.length-1].toString().replace(regex,"")===""){
            list.length= list.length-1;
        }
        for (let i=0;i<list.length;i++){
          let block=    new Block();
          block.decode(list[i]);
          this.list.push(block);
        }
      
    }
    outData(){
        let _this=this;
        if(  this.list.length==0){
            return [];
        }

        let res=""
        let buffList=[];
        for(let i=0;i<_this.list.length;i++){
             let row =this.list[i].encoded();
             res+=row+'c'
        }
        let dataList =res.split("");
        if(dataList.length%2==1){ //如果如果长度单数，则在最后补1
            dataList=["0",...dataList]
        }
       for(let i=0;i<dataList.length;i=i+2){
           let num1 = parseInt(dataList[i], 16).toString(2);
           let test1 =(Array(4).join(0) + num1).slice(-4)
           let num2 = parseInt(dataList[i+1], 16).toString(2);
           let test2 =(Array(4).join(0) + num2).slice(-4)
            let text = test1+test2;
            let intcode= parseInt(text,2)
            buffList.push( intcode)
        }
        return new Uint8Array(buffList);
    }

    /**
     * 查找 如果rowA 存在
     * 那么 返回 rowB 里面的rowC
     * @param rowA
     * @param rowB
     * @param rowC
     */
   isBlock(rowA,rowB,rowC){
        let isRes=0
       for(let i=0;i<this.list.length;i++){
          if(this.list[i].isRows(...rowA).length>0){
           isRes=1;
           break;
          }
       }
        for(let i=0;i<this.list.length;i++){
            if(this.list[i].isRows(...rowB).length>0){
                return this.list[i].isRows(...rowC);
            }
        }

    }

    saveData(fileId,blockId,rowId,data){
        if(blockId!=null){

            return  this.list[fileId].saveData(fileId,blockId,rowId,data);
        }else {
            this.list[fileId]=data;
        }
    }

    addData(fileId,blockId,rowId,data){
        if(blockId!=null){

          return  this.list[blockId].addData(fileId,blockId,rowId,data);
        }else {
           let index= this.list.length;
            this.list.push(data)
           return index;
        }
    }
    addBlock(data){
       this.index=this.index+1;
       this.list.push(new Block());
    }
    addRow(row){
       this.list[this.list.length-1].addRow(row);
    }
    findIndex(){
       this.index;
    }
    findData(FileId,BlockId,RowId,func){
       let _this=this;
        if(BlockId!==null){
            return   this.list[FileId].findData(FileId,BlockId,RowId,func)
        }
        if(func){
            return func(FileId,BlockId,RowId,_this);
        }

        return this.list[FileId];

    }

}
